<script setup name="登录">
import { onMounted, ref } from "vue";
import { User, Lock, View, Hide } from "@element-plus/icons-vue";
import { userLoginService } from "@/server/user";
import { useRouter } from "vue-router";
import useUserInfoStore from "../stores/userInfo.js";
import { ElMessage } from "element-plus";
const router = useRouter();
const userInfoStore = useUserInfoStore();
const loginForm = ref({
  username: "",
  password: "",
});
const rememberMe = ref(false); // 用于记录用户是否选择记住我
onMounted(() => {
  const savedUsername = localStorage.getItem("username");
  const savedPassword = localStorage.getItem("password");
  if (savedUsername && savedPassword) {
    loginForm.value.username = savedUsername;
    loginForm.value.password = savedPassword;
    rememberMe.value = true;
  }
});
const login = async () => {
  try {
    if (loginForm.value.username == null || loginForm.value.username === "") {
      ElMessage.error("请输入用户名");
      return;
    } else if (
      loginForm.value.password == null ||
      loginForm.value.password === ""
    ) {
      ElMessage.error("请输入密码");
      return;
    }
    // 如果选择了记住我，将用户名和密码保存到 localStorage
    if (rememberMe.value) {
      localStorage.setItem("username", loginForm.value.username);
      localStorage.setItem("password", loginForm.value.password);
    } else {
      // 如果没有选择记住我，清除可能已保存的用户名和密码
      localStorage.removeItem("username");
      localStorage.removeItem("password");
    }

    let result = await userLoginService(loginForm.value);
    userInfoStore.setInfo(result.username);

    await router.push("/index/iIndex");
  } catch (e) {
    confirm("请确认您的账号密码未输入错误 ");
    ElMessage.error("登录失败");
  }
};
const showPassword = ref(false);
const togglePassword = () => {
  showPassword.value = !showPassword.value;
};

const register = () => {
  router.push("/register");
};
</script>

<template>
  <div class="background">
    <div style="margin-left: 40%">
      <div class="form">
        <el-form-item>
          <img src="@/assets/pictor/tcl.png" width="80" height="50" />
          <h1 style="font-size: 50px;margin-left: 15px">登录后台</h1>
        </el-form-item>
        <el-form style="width: 100%" status-icon :model="loginForm">
          <el-form-item prop="username" class="form-item">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              type="text"
              style="width: 100%; height: 80px; font-size: 30px"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password" class="form-item">
            <el-input
              :prefix-icon="Lock"
              v-model="loginForm.password"
              placeholder="请输入密码"
              :type="showPassword ? 'text' : 'password'"
              style="width: 100%; height: 80px; font-size: 30px"
            >
              <template #append>
                <el-icon v-if="showPassword" @click="togglePassword">
                  <View
                /></el-icon>
                <el-icon v-else @click="togglePassword"> <Hide /> </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <div class="flex">
              <el-checkbox>记住我</el-checkbox>
              <div class="fontdiv" style="display: flex;">
                <el-button
                  type="text"
                  style="font-size: 16px; margin-right: 10px; color: #55aaff"
                  @click="register"
                >
                  注册
                </el-button>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              class="button"
              type="primary"
              auto-insert-space
              @click="login"
              style="
                width: 80%;
                height: 80px;
                margin-left: 65px;
                font-size: 30px;
              "
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>

.el-input__suffix {
  display: none;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  /* 确保所有元素的内边距和边框不会影响宽度 */
}

html,
body {
  height: 100%;
  /* 确保 html 和 body 高度为 100% */
}

.background {
  background-image: url("@/assets/pictor/login.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
.form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 600px;
  padding: 60px;
  border-radius: 8px;
  background-color: #fff;

  .title {
    text-align: center;
    margin-bottom: 20px;
  }

  .button {
    width: 100%;
    margin-top: -20px;
  }

  .el-form-item {
    margin-bottom: 50px;
    /* 增加表单项之间的间距 */
  }

  .flex {
    width: 100%;
    height: 10px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 50px;
    margin-top: -50px;
  }
}
</style>
